package com.example.function_interface;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 查看网址https://www.runoob.com/java/java8-functional-interfaces.html
 */
public class ClientTest {

    @Test
    public void myTest() throws Exception{
        List<User> userList = new ArrayList<>();
        userList.add(new User(1, "张三", 12, false));
        userList.add(new User(2, "李四", 22, false));
        userList.add(new User(3, "王五", 32, true));
        userList.add(new User(4, "郑静", 2, true));

        //成年人
        List<User> adultUserList = new ArrayList<>();
        UserPredicate userPredicate = new UserPredicate() {
            @Override
            public boolean filter(User user) {
                return user.getAge() > 18;
            }
        };
        for (User user : userList) {
            if (userPredicate.filter(user)) {
                adultUserList.add(user);
            }
        }
        String nameStr = String.join(",", adultUserList.stream().map(User::getName).collect(Collectors.toList()));
        System.out.println("成年人："+nameStr);

        //男人
        List<User> manUserList = new ArrayList<>();
        //lambda 表达式
        userPredicate = user -> user.isMan();
        for (User user : userList) {
            if (userPredicate.filter(user)) {
                manUserList.add(user);
            }
        }
        nameStr = manUserList.stream().map(User::getName).collect(Collectors.joining(","));
        System.out.println("男人："+nameStr);
    }
}
